//
//  跳表.swift
//  Swift-LeetCode
//
//  Created by 卢悦明 on 2024/3/6.
//

import UIKit

fileprivate class SkipNode<K: Comparable, V: Comparable> {
    let key: K
    let value: V
    var nextArray: [SkipNode<K, V>] = []
    init(_ key: K, _ value: V) {
        self.key = key
        self.value = value
    }
}
/**
 只写了一部分，需要的话，需要看是视频写一下
 */
class SkipList<K: Comparable, V: Comparable> {
    
    private var size = 0
    private var level = 0
    private var first: SkipNode<K, V>
    
    init(_ key: K, _ value: V) {
        first = SkipNode<K,V>(key, value)
    }
    
    /// 是否为空
    /// - Returns: 结果
    func isEmpty() -> Bool {
        size == 0
    }
    
    /// 通过key获取value
    /// - Parameter key: key
    /// - Returns: 内容
    func get(_ key: K) -> V? {
        var node = first
        for i in (0..<level).reversed() {
            while node.nextArray.count > i && key > node.nextArray[i].key {
                node = node.nextArray[i]
            }
            if key == node.nextArray[i].key {
                return node.nextArray[i].value
            }
        }
        return nil
    }
    
    
    /// 添加节点
    /// - Parameters:
    ///   - key: key
    ///   - value: 值
    /// - Returns:
    func put(_ key: K, _ value: V) -> V {
        value
    }
    
    /// 删除节点
    /// - Parameter key: key
    /// - Returns: 删除节点的值
    func remove(_ key: K) -> V? {
        nil
    }
}
